package com.tansun.easycare.rule.ml.algorithm.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.rule.ml.algorithm.entity.AlogrithmVariables;
import com.tansun.easycare.rule.ml.algorithm.entity.DataVariable;
import com.tansun.easycare.rule.ml.algorithm.entity.DataVariableList;
import com.tansun.easycare.rule.ml.algorithm.entity.DatabaseConn;
import com.tansun.easycare.rule.ml.algorithm.entity.PyResponse;
import com.tansun.easycare.rule.ml.algorithm.entity.StockVariables;
import com.tansun.easycare.rule.ml.algorithm.entity.TicketVariables;
import com.tansun.easycare.rule.ml.algorithm.service.IAlgorthmService;
import com.tansun.easycare.rule.ml.model.entity.MlDataModel;
import com.tansun.easycare.rule.ml.model.service.MlDataModelService;


/**
 * 数据处理控制类
 * @author xch
 * @version 2018-08-24
 */
@Controller
@RequestMapping(value = "/ml/dataInput")
public class DataProcessController extends BaseController{
	
	@Autowired
	private MlDataModelService mlDataModelService;

	@Autowired
	private IAlgorthmService iAlgrthmService;
	
	@ModelAttribute
	public MlDataModel get(@RequestParam(required=false) String id) {
		MlDataModel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mlDataModelService.get(id);
		}
		if (entity == null){
			entity = new MlDataModel();
		}
		return entity;
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/data_db_import")
	public String dataDBImport(MlDataModel mlDataModel,Model model) {
		model.addAttribute("mlDataModel", mlDataModel);
		return "rule/ml/data_db_import";
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/data_cvs_import")
	public String dataCvsImport(MlDataModel mlDataModel,Model model) {
		model.addAttribute("mlDataModel", mlDataModel);
		return "rule/ml/data_cvs_import";
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/data_excel_import")
	public String dataExcelImport(MlDataModel mlDataModel,Model model) {
		model.addAttribute("mlDataModel", mlDataModel);
		return "rule/ml/data_excel_import";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/data_stock_import")
	public String dataStockImport(MlDataModel mlDataModel,Model model) {
		model.addAttribute("mlDataModel", mlDataModel);
		return "rule/ml/data_stock_import";
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/loadStockData")
	public @ResponseBody JSONObject loadStockData(MlDataModel mlDataModel,StockVariables stockVariables, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.loadStockData(mlDataModel,stockVariables);
		JSONObject resData = (JSONObject)resp.getResponse().get("loadStockData");
		
		mlDataModel.setDataFile(resData.getString("savepath"));
		mlDataModelService.updateDataFile(mlDataModel);
		
		addMessage(redirectAttributes, "修改数据集成功");
		return resData;
	}

	
	@RequestMapping(method = RequestMethod.GET, value = "/data_ticket_import")
	public String dataTicketImport(MlDataModel mlDataModel,Model model) {
		model.addAttribute("mlDataModel", mlDataModel);
		return "rule/ml/data_ticket_import";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/data_feature_import")
	public String dataFeatureImport(MlDataModel mlDataModel,Model model) {
		PyResponse resp = iAlgrthmService.getFeatureProject();
		String resData = resp.getResponse().get("getFeatureProject").toString();
		List<MlDataModel> models = JSONArray.parseArray(resData, MlDataModel.class);
		List<MlDataModel> mlDataModelList = new ArrayList<MlDataModel>();
		for(MlDataModel mlmodel: models) {
			MlDataModel newmlDataModel = mlDataModelService.findByModelId(mlmodel);
			if(newmlDataModel!=null) {
				newmlDataModel.setResultFile(mlmodel.getResultFile());
				mlDataModelList.add(newmlDataModel);
			}
		}
		model.addAttribute("mlDataModel", mlDataModel);
		model.addAttribute("mlDataModelList", mlDataModelList);
		return "rule/ml/data_feature_import";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/updateDataFile")
	public @ResponseBody JSONObject updateDataFile(MlDataModel mlDataModel, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.dataLoad(mlDataModel);
		JSONObject resData = (JSONObject)resp.getResponse();
		mlDataModelService.updateDataFile(mlDataModel);
		
		addMessage(redirectAttributes, "修改数据集成功");
		return resData;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/loadTicketData")
	public @ResponseBody JSONObject loadTicketData(MlDataModel mlDataModel,TicketVariables ticketVariables, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.loadTicketData(mlDataModel,ticketVariables);
		JSONObject resData = (JSONObject)resp.getResponse().get("loadTicketData");
		
		mlDataModel.setDataFile(resData.getString("savepath"));
		mlDataModelService.updateDataFile(mlDataModel);
		
		addMessage(redirectAttributes, "修改数据集成功");
		return resData;
	}
	
	//获取数据表 
	@RequestMapping(method = RequestMethod.GET, value = "/getTabels")
	public @ResponseBody List<String> getTabels(DatabaseConn databaseConn) {
		List<String> tabelList = databaseConn.queryTabels();
		return tabelList;
	}
	//导入数据表 
	@RequestMapping(method = RequestMethod.GET, value = "/loadDataByDBTabel")
	public @ResponseBody JSONObject loadDataByDBTabel(MlDataModel mlDataModel,DatabaseConn databaseConn,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.loadDataByDBTabel(mlDataModel,databaseConn);
		JSONObject resData = (JSONObject)resp.getResponse().get("loadDataByDBTabel");
		
		mlDataModel.setDataFile(resData.getString("savepath"));
		mlDataModelService.updateDataFile(mlDataModel);
		
		addMessage(redirectAttributes, "修改数据集成功");
		return resData;
	}
	
	//获取数据统计
	@RequestMapping(method = RequestMethod.GET, value = "/getDataStatistics")
	public @ResponseBody JSONObject getDataStatistics(MlDataModel mlDataModel,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.getDataStatistics(mlDataModel);
		JSONObject dataVariable  = (JSONObject)resp.getResponse().get("DataStatistics");
		addMessage(redirectAttributes, "获取统计数据成功");
		return dataVariable;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/deleteMissingValue")
	public @ResponseBody JSONObject deleteMissingValue(MlDataModel mlDataModel,DataVariableList dataVriables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.deleteMissingValue(mlDataModel,dataVriables);
		JSONObject dataVariable = (JSONObject)resp.getResponse().get("deleteMissingValue");
		addMessage(redirectAttributes, "缺失值删除成功");
		return dataVariable;
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/deleteAllNa")
	public @ResponseBody JSONObject deleteAllNa(MlDataModel mlDataModel,DataVariableList dataVriables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.deleteAllNa(mlDataModel,dataVriables);
		JSONObject dataVariable = (JSONObject)resp.getResponse().get("deleteAllNa");
		addMessage(redirectAttributes, "缺失值删除成功");
		return dataVariable;
	}
	@RequestMapping(method = RequestMethod.GET, value = "/replaceMissingValue")
	public @ResponseBody JSONObject replaceMissingValue(MlDataModel mlDataModel,DataVariableList dataVriables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.replaceMissingValue(mlDataModel,dataVriables);
		JSONObject dataVariable = (JSONObject)resp.getResponse().get("replaceMissingValue");
		addMessage(redirectAttributes, "缺失值替换成功");
		return dataVariable;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/replaceAllNa")
	public @ResponseBody JSONObject replaceAllNa(MlDataModel mlDataModel,DataVariableList dataVriables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.replaceAllNa(mlDataModel,dataVriables);
		JSONObject dataVariable = (JSONObject)resp.getResponse().get("replaceAllNa");
		System.out.println(dataVariable);
		addMessage(redirectAttributes, "缺失值替换成功");
		return dataVariable;
	}
	
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/deleteTable")
	public @ResponseBody JSONObject deleteTable(MlDataModel mlDataModel,String tableName,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.deleteTable(mlDataModel,tableName);
		JSONObject dataVariable = (JSONObject)resp.getResponse().get("deleteTable");
		addMessage(redirectAttributes, "缺失值替换成功");
		return dataVariable;
	}
	
	/**
	 * 修改数据
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/dataModify")
	public @ResponseBody JSONObject dataModify(MlDataModel mlDataModel,DataVariableList dataVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.dataModify(mlDataModel, dataVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("dataModify");
		addMessage(redirectAttributes, "修改数据集成功");
		return imgData;
	}
	
	/**
	 * 数据集变量选取
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/dataVariablesSetup")
	public @ResponseBody JSONObject dataVariablesSetup(MlDataModel mlDataModel,DataVariableList dataVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.dataVariablesSetup(mlDataModel, dataVariables);
		JSONObject dataSetreturn = (JSONObject)resp.getResponse().get("dataVariablesSetup");
		addMessage(redirectAttributes, "数据集变量选取");
		return dataSetreturn;
	}
	
	
	@RequestMapping(method = RequestMethod.GET, value = "/setUsedType")
	public @ResponseBody JSONObject setUsedType(MlDataModel mlDataModel,AlogrithmVariables alogrithmVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.setUsedType(mlDataModel, alogrithmVariables);
		addMessage(redirectAttributes, "数据用途设置");
		return null;
	}
	/**
	 * 获取算法参数设置
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/getParameter")
	public @ResponseBody JSONObject getParameter(MlDataModel mlDataModel,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.getParameter(mlDataModel);
		JSONObject dataSetreturn =(JSONObject)resp.getResponse().get("getParameter");
		addMessage(redirectAttributes, "获取算法参数设置");
		return dataSetreturn;
	}
	
	/**
	 * 目标变量标签化
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/dataTargetVarsLabelSetup")
	public @ResponseBody JSONObject dataTargetVarsLabelSetup(MlDataModel mlDataModel,DataVariableList dataVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.dataTargetVarsLabelSetup(mlDataModel, dataVariables);
		JSONObject tarVarLab = (JSONObject)resp.getResponse().get("dataTargetVarsLabelSetup");
		addMessage(redirectAttributes, "目标变量标签化");
		return tarVarLab;
	}
	
	/**
	 * 数据集平衡设置
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/dataBanlanceSetup")
	public @ResponseBody JSONObject dataBanlanceSetup(MlDataModel mlDataModel,DataVariable dataVariable,RedirectAttributes redirectAttributes,Model model) {
		DataVariableList dataVariables = new DataVariableList();
		dataVariables.setTargetVars(dataVariable);	
		PyResponse resp = iAlgrthmService.dataBanlanceSetup(mlDataModel, dataVariables);
		JSONObject tarVarLab = (JSONObject)resp.getResponse().get("dataBanlanceSetup");
		addMessage(redirectAttributes, "数据集平衡设置成功");
		return tarVarLab;
	}
	/**
	 * 数据集分割设置，分为训练集和测试集
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/dataSplitSetup")
	public @ResponseBody JSONObject dataSplitSetup(MlDataModel mlDataModel,DataVariableList dataVariables,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.dataSplitSetup(mlDataModel, dataVariables);
		JSONObject imgData = (JSONObject)resp.getResponse().get("dataSplitSetup");
		addMessage(redirectAttributes, "数据集分割设置成功");
		return imgData;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getInputArgsMsg")
	public @ResponseBody JSONArray getInputArgsMsg(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getInputArgsMsg(mlDataModel);
		JSONArray argsMsg = (JSONArray)resp.getResponse().get("getInputArgsMsg");
		addMessage(redirectAttributes, "获取参数信息成功");
		return argsMsg;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/getDataSetInputMsg")
	public @ResponseBody JSONObject getDataSetInputMsg(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getDataSetInputMsg(mlDataModel);
		JSONObject argsMsg = (JSONObject)resp.getResponse().get("getDataSetInputMsg");
		addMessage(redirectAttributes, "获取参数信息成功");
		return argsMsg;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/getVariableMsg")
	public @ResponseBody JSONObject getVariableMsg(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getVariableMsg(mlDataModel);
		JSONObject argsMsg = (JSONObject)resp.getResponse().get("getVariableMsg");
		addMessage(redirectAttributes, "获取参数信息成功");
		return argsMsg;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/modelTrainDel")
	public @ResponseBody JSONArray modelTrainDel(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		iAlgrthmService.modelTrainDel(mlDataModel);
		addMessage(redirectAttributes, "删除已训练模型参数成功");
		return null;
	}
	@RequestMapping(method = RequestMethod.GET, value = "/getTrainData")
	public @ResponseBody JSONObject getTrainData(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getTrainData(mlDataModel);
		JSONObject argsMsg = (JSONObject)resp.getResponse().get("getTrainData");
		addMessage(redirectAttributes, "获取训练结果成功");
		return argsMsg;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/getTransferData")
	public @ResponseBody JSONObject getTransferData(MlDataModel mlDataModel,RedirectAttributes redirectAttributes,Model model) {
		PyResponse resp = iAlgrthmService.getTransferData(mlDataModel);
		JSONObject jsonobj = (JSONObject)resp.getResponse().get("getTransferData");
		addMessage(redirectAttributes, "初始化转化数据成功");
		return jsonobj;
	}
	
}
